<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of layout_lib
 *
 * @author Administrator
 */
class Layout_lib {

  protected $ci;

  public function __construct() {
    $this->ci = &get_instance();
  }

  protected function get_menu($menu_key = '', $inner = '', $class='parent_menu') {
    $html = '';
    $parent_menu = $this->ci->db->get_where(DB_TABLE_MENU, array('name' => $menu_key), 1, 0)->result();
    if (!empty($parent_menu)) {   
      $this->ci->db->order_by('order', 'asc');
      $menus = $this->ci->db->get_where(DB_TABLE_MENU, array('parent_id' => $parent_menu[0]->id))->result();
      foreach ($menus as $menu) {
        $pages = array();
        $this->ci->db->order_by('order', 'asc');
        $children = $this->ci->db->get_where(DB_TABLE_MENU, array('parent_id' => $menu->id))->result();
        foreach($children as $child){
          $page = $this->ci->db->get_where(DB_TABLE_PAGE, array('id' => $child->page_id), 1, 0)->result();
          if(!empty($page)){
            $pages[] = $page[0]->name;
          }
        }
        $temp = $inner;
        $page = $this->ci->db->get_where(DB_TABLE_PAGE, array('id' => $menu->page_id), 1, 0)->result();
        if (!empty($page)) {
          $temp = str_replace('item.page', $page[0]->name, $temp);
          $temp = str_replace('{{current_page:active}}', $this->ci->router->fetch_class() == $page[0]->name ? "active ".$class : $class, $temp);
        } else {
          $temp = str_replace('{{current_page:active}}', $class, $temp);
          $temp = str_replace('{{link:item.page}}', empty($menu->url) ? '#' : $menu->url, $temp);
        }
        $temp = str_replace('{{item.description}}', $menu->description, $temp);
        $temp = str_replace('{{item.name}}', $menu->name, $temp);
        $html .= $temp;
        if ((!empty($page) && $this->ci->router->fetch_class() == $page[0]->name) || in_array($this->ci->router->fetch_class(), $pages) ) {
          $temp_html = $this->get_menu($menu->name, $inner,'child_menu');
          if (!empty($temp_html['code_html'])) {
            $html .= $temp_html['code_html'];
          }
        }
      }
      $html = array('code_html' => $html);
      $html = $this->process_code($html);
    }
    return $html;
  }

  public function process_code($layout) {

    if (!empty($layout['theme_path'])) {
      $layout['code_html'] = str_replace('{{theme_path}}', base_url() . APPPATH .
              'themes/' . $layout['theme_path'], $layout['code_html']);
    }
    while (strpos($layout['code_html'], '{{layout') !== false) {
      $i = strpos($layout['code_html'], '{{layout');
      $temp = substr($layout['code_html'], $i);
      $j = strpos($temp, '}}');
      $ref_layout = substr($temp, 0, $j + 2);
      if (str_startsWith($ref_layout, '{{layout:') && str_endsWith($ref_layout, '}}')) {
        $ref = substr($ref_layout, 9, count($ref_layout) - 3);
        $ref_obj = $this->ci->db->get_where(DB_TABLE_LAYOUT, array('name' => $ref), 1, 0)->result_array();
        if (!empty($ref_obj)) {
          $ref_obj = $this->process_code($ref_obj[0]);
          $layout['code_html'] = str_replace($ref_layout, $ref_obj['code_html'], $layout['code_html']);
        } else {
          $layout['code_html'] = str_replace($ref_layout, '', $layout['code_html']);
        }
      } else {
        $layout['code_html'] = str_replace($ref_layout, '', $layout['code_html']);
      }
    }
    while (strpos($layout['code_html'], '{{for:menu:') !== false) {
      $i = strpos($layout['code_html'], '{{for:menu:');
      $temp = substr($layout['code_html'], $i);
      $k = strpos($temp, '}}');
      $j = strpos($temp, '{{end_for}}');
      $for = substr($temp, 0, $j + 11);
      $menu_key = substr($temp, 11, $k - 11);
      $inner = substr($temp, $k + 2, $j - $k - 2);
      $html = $this->get_menu($menu_key, $inner);
      if (!empty($html)) {
        $layout['code_html'] = str_replace($for, $html['code_html'], $layout['code_html']);
      } else {
        $layout['code_html'] = str_replace($for, '', $layout['code_html']);
      }
    }

    while (strpos($layout['code_html'], '{{link:') !== false) {
      $i = strpos($layout['code_html'], '{{link:');
      $temp = substr($layout['code_html'], $i);
      $j = strpos($temp, '}}');
      $link = substr($temp, 0, $j + 2);
      $uri = substr($temp, 7, $j - 7);
      $uri = site_url('pages/' . $uri);
      $layout['code_html'] = str_replace($link, $uri, $layout['code_html']);
    }
    
    while (strpos($layout['code_html'], '{{media:') !== false) {
      $i = strpos($layout['code_html'], '{{media:');
      $temp = substr($layout['code_html'], $i);
      $j = strpos($temp, '}}');
      $link = substr($temp, 0, $j + 2);
      $file_name = substr($temp, 7, $j - 8);
      $file_name = site_url('file_upload/get_img_from_path?path=' . $file_name);
      $layout['code_html'] = str_replace($link, $uri, $layout['code_html']);
    }

    return $layout;
  }

}